package com.example.cinema.activity;

import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.cinema.R;
import com.example.cinema.request.RequestSend;

import java.util.regex.Pattern;

public class ActivityRetrieve  extends AppCompatActivity {
    private EditText phome;
    private Button signbtn;
    private ImageView finss;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
        phome = findViewById(R.id.phone);
        signbtn = findViewById(R.id.signbtn);
        finss = findViewById(R.id.finsh);
        init();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    private void init() {
        //发送找回
        signbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String p = phome.getText().toString();
                if(p.isEmpty())
                {
                    Toast.makeText(getApplicationContext(),"手机号码错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean bo = IsPhone(p);
                if(!bo)
                {
                    Toast.makeText(getApplicationContext(),"非正常手机号码",Toast.LENGTH_SHORT).show();
                    return;
                }

                new Thread()
                {
                    @Override
                    public void run() {
                        RequestSend send = new RequestSend(getApplicationContext());
                        boolean bool = send.Retrieve(p);
                        Message message = new Message();
                        message.what =1000;
                        message.obj = bool;
                        handler.sendMessage(message);
                    }
                }.start();

            }
        });
        //返回
        finss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private boolean IsPhone(String phone)
    {
        String pat = "^1[3|4|5|7|8][0-9]\\d{4,8}$";
        return Pattern.matches(pat,phone);
    }


    Handler handler = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1000)
            {
                boolean b  = (boolean) msg.obj;
                if(b)
                {
                    Toast.makeText(getApplicationContext(),"已发送短信到您的手机",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getApplicationContext(),"找回密码失败",Toast.LENGTH_SHORT).show();
                }
            }
        }
    };
}
